﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using MINERP.COM;
using MINERP.DAL;
using MINERP.MODEL.FoundationModel;
using MINERP.MODEL.PaperManager;

namespace MINERP.BLL.Common
{
    public class WastePaperManageBLL
    {
        /// <summary>
        /// 取得所有的未确认的浪费记录
        /// </summary>
        /// <returns></returns>
        public List<PaperWasteRecord_info> GetUnSureWasteRecord()
        {

            List<PaperWasteRecord_info> rtnList = new List<PaperWasteRecord_info>();
            PaperWasteRecord_info recordInfo;
            int unSureStatus = (int)WasterPaperStatus.unSure;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                //recordInfo=new PaperWasteRecord_info();
                List<PaperWasteRecords> records = db.PaperWasteRecords.Where(p => p.Status == unSureStatus).ToList();
                foreach (PaperWasteRecords list in records)
                {
                    recordInfo = new PaperWasteRecord_info();
                    recordInfo.Id = list.Id;
                    recordInfo.CreateTime = list.CreateTime;
                    recordInfo.Length = list.Length;
                    list.PaperInfoReference.Load();
                    PaperInfo_Info paperinfo = new PaperInfo_Info();

                    {
                        paperinfo.AddTime = (DateTime)list.PaperInfo.AddTime;
                        paperinfo.EndPaperLength =(double) list.PaperInfo.EndPaperLength;
                        paperinfo.InvalidationArea = list.PaperInfo.PaperBreakArea;
                        paperinfo.LastBordId = list.PaperInfo.LastBordId;
                        paperinfo.LastLength = (double)list.PaperInfo.LastLength;
                        paperinfo.LastPrintTime = (DateTime)list.PaperInfo.LastPrintTime;
                        paperinfo.Length = (double)list.PaperInfo.Length;
                        ;
                        paperinfo.PaperConsumptionLength = list.PaperInfo.PaperConsumptionLength;

                        paperinfo.PaperEndArea = list.PaperInfo.PaperEndArea;
                        paperinfo.PrintPaperId = list.PaperInfo.PrintPaperId;
                    }
                    recordInfo.PaperInfo = paperinfo;
                    if (!list.PaperWasteTypeReference.IsLoaded)
                    {
                        list.PaperWasteTypeReference.Load();
                    }
                    recordInfo.PaperWasteType = new PaperWasteType_info() { Id = list.PaperWasteType.Id, PaperOperationTypeName = list.PaperWasteType.PaperOperationTypeName };
                    recordInfo.Remark = list.Remark;
                    recordInfo.Status = list.Status;
                    recordInfo.Width = list.Width;

                    rtnList.Add(recordInfo);
                }

            }
            return rtnList;
        }
        /// <summary>
        /// 取得时间段内所有的未确认的浪费记录
        /// </summary>
        /// <returns></returns>
        public List<PaperWasteRecord_info> GetUnSureWasteRecord(DateTime inDateStart, DateTime inDateEnd)
        {

            List<PaperWasteRecord_info> rtnList = new List<PaperWasteRecord_info>();
            PaperWasteRecord_info recordInfo;
            int unSureStatus = (int)WasterPaperStatus.unSure;
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                //recordInfo=new PaperWasteRecord_info();
                List<PaperWasteRecords> records = db.PaperWasteRecords.Where(p => p.CreateTime > inDateStart && p.CreateTime < inDateEnd).ToList();
                foreach (PaperWasteRecords list in records)
                {
                    recordInfo = new PaperWasteRecord_info();
                    recordInfo.Id = list.Id;
                    recordInfo.CreateTime = list.CreateTime;
                    recordInfo.Length = list.Length;
                    list.PaperInfoReference.Load();
                    PaperInfo_Info paperinfo = new PaperInfo_Info();

                    {
                        paperinfo.AddTime = (DateTime)list.PaperInfo.AddTime;
                        paperinfo.EndPaperLength = (double)list.PaperInfo.EndPaperLength;
                        paperinfo.InvalidationArea = list.PaperInfo.PaperBreakArea;
                        paperinfo.LastBordId = list.PaperInfo.LastBordId;
                        paperinfo.LastLength =(double) list.PaperInfo.LastLength;
                        paperinfo.LastPrintTime = (DateTime)list.PaperInfo.LastPrintTime;
                        paperinfo.Length =(double) list.PaperInfo.Length;
                        ;
                        paperinfo.PaperConsumptionLength = list.PaperInfo.PaperConsumptionLength;

                        paperinfo.PaperEndArea = list.PaperInfo.PaperEndArea;
                        paperinfo.PrintPaperId = list.PaperInfo.PrintPaperId;
                    }
                    recordInfo.PaperInfo = paperinfo;
                    if (!list.PaperWasteTypeReference.IsLoaded)
                    {
                        list.PaperWasteTypeReference.Load();
                    }
                    recordInfo.PaperWasteType = new PaperWasteType_info() { Id = list.PaperWasteType.Id, PaperOperationTypeName = list.PaperWasteType.PaperOperationTypeName };
                    recordInfo.Remark = list.Remark;
                    recordInfo.Status = list.Status;
                    recordInfo.Width = list.Width;

                    rtnList.Add(recordInfo);
                }

            }
            return rtnList;
        }

        /// <summary>
        /// 确认
        /// </summary>
        /// <param name="inWasteId"></param>
        /// <returns></returns>
        public int MakeSureRecordSinger(int inWasteId)
        {
            try
            {


                using (newSampleYPDBEntities db = new newSampleYPDBEntities())
                {
                    PaperWasteRecords list = db.PaperWasteRecords.FirstOrDefault(p => p.Id == inWasteId);
                    if (list == null)
                    {
                        return 0;
                    }

                    if (list.Status != (int)WasterPaperStatus.Sure)
                    {
                        list.Status = (int)WasterPaperStatus.Sure;
                    }
                    else
                    {
                        return 2;
                    }

                    db.SaveChanges();

                }
            }
            catch
            {

                return -1;
            }
            return 1;

        }
    }
}
